package com.bamboowind.consumer.routing;

import com.bamboowind.consumer.util.RabbitMqUtils;
import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * 路由模式：消费者1
 *
 * @author cpw
 */
public class ConsumerRouting1 {
    private final static String QUEUE_NAME1 = "rabbitmq-directQueue1";
    private final static String QUEUE_NAME2 = "rabbitmq-directQueue2";

    public static void main(String[] args) throws IOException {

        //3.创建连接Connection
        Connection connection = RabbitMqUtils.getConnection();

        //4.创建Channel
        Channel channel = connection.createChannel();

        //6.接收消息
        /**
         * basicConsume(String queue, boolean autoAck, Consumer callback)
         * 参数:
         *      1.queue:队列名称
         *      2.autoAck:是否自动确认
         *      3.callback:回调对象
         * */
        Consumer consumer = new DefaultConsumer(channel) {
            /**
             * 回调方法，当收到消息后会自动执行该方法
             * 1.consumerTag:标识
             * 2.envelope:获取一些信息，交换机，路由等
             * 3.properties:配置信息
             * 4.body:数据
             * */
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {

                System.out.println("body:" + new String(body));
                System.out.println("将日志信息打印到控制台...");
            }
        };
        channel.basicConsume(QUEUE_NAME2, true, consumer);
    }
}
